package com.xiaoyg.algorithm;

import lombok.Data;
import com.xiaoyg.algorithm.ReverseList.ListNode;

@Data
public class DeleteDuplicates {

    public static void main(String[] args) {
        ListNode node5 = new ListNode(1,null);
        ListNode node4 = new ListNode(1, node5);
        ListNode node3 = new ListNode(2, node4);
        ListNode node2 = new ListNode(2, node3);
        ListNode node1 = new ListNode(3, node2);
        System.out.println(node1);
        ListNode recursion = deleteDuplicates2(node1);
        System.out.println(recursion);
    }

    public static ListNode deleteDuplicates1(ListNode head){
        if (head == null) return null;
        ListNode current = head;
        while (current.next != null){
            if (current.val == current.next.val){
                current.next = current.next.next;
            }else {
                current = current.next;
            }
        }
        return head;
    }

    public static ListNode deleteDuplicates2(ListNode head){
        if (head == null || head.next == null) return head;
        head.next = deleteDuplicates2(head.next);
        return head.val==head.next.val? head.next:head;
    }

    public static ListNode deleteDuplicates(ListNode head) {

//        if(head == null || head.next == null){
//            return head;
//        }
//        //头结点和后一个时候相等
//        if(head.val == head.next.val){
//            //去掉头结点
//            return deleteDuplicates(head.next);
//        }else{
//            //加上头结点
//            head.next = deleteDuplicates(head.next);
//            return head;
//        }

        if(head == null || head.next == null){

            // head = 1
            return head;
        }
        //head.next = 1
        head.next = deleteDuplicates(head.next);

        if(head.val == head.next.val)
            head = head.next;
        return head;
    }

//
//    public static ListNode deleteDuplicates(ListNode head) {
//        //baseCase
//        if (head == null || head.next == null) {
//            return head;
//        }
//
//        ListNode next = head.next;
//        //如果是这种情况
//        //      1 --> 1 --> 1 --> 2 --> 3
//        //     head  next
//        //1.则需要移动next直到出现与当前head.value不相等的情况（含null）
//        //2.并且此时的head已经不能要了，因为已经head是重复的节点
//        //--------------else-------------
//        //      1 --> 2 --> 3
//        //     head  next
//        //3.如果没有出现1的情况，则递归返回的节点就作为head的子节点
//        if (head.val == next.val) {
//            //1
//            while (next != null && head.val == next.val) {
//                next = next.next;
//            }
//            //2
//            head = deleteDuplicates(next);
//        } else {
//            //3
//            head.next = deleteDuplicates(next);
//        }
//        return head;
//    }




}
